package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.AppVersion;
import com.dingreading.cloud.admin.entity.HeadStaff;
import com.dingreading.cloud.admin.service.AppVersionService;
import com.dingreading.cloud.admin.service.HeadStaffService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 总部-书种管理相关
 *
 * @author xiaofan
 */
@RestController
@RequestMapping("/system/appVersion")
@Api(tags = "总部-APP版本管理")
public class AppVersionController {

    @Resource
    private AppVersionService appVersionService;
    @Resource
    private HeadStaffService headStaffService;

    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "app类型 1.Android 2.iOS") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(value = "是否强制更新 1.是 0.不是") @RequestParam(value = "forceStatus", required = false) Integer forceStatus,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<AppVersion> page = appVersionService.pageList(pageUtil, type, forceStatus, enabled);
        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "新增/编辑信息")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "手机系统 1.安卓 2.ios") @RequestParam(value = "os") Integer os,
            @ApiParam(value = "版本号") @RequestParam(value = "versionCode") Integer versionCode,
            @ApiParam(value = "版本名") @RequestParam(value = "versionName") String versionName,
            @ApiParam(value = "下载地址") @RequestParam(value = "downloadUrl") String downloadUrl,
            @ApiParam(value = "更新内容") @RequestParam(value = "content") String content,
            @ApiParam(value = "是否强制更新 1.是 0.不是") @RequestParam(value = "forceStatus") Integer forceStatus,
            @ApiParam(value = "门店使用权限  1.自营+加盟 2.自营 3.加盟店") @RequestParam(value = "selfStatus") Integer selfStatus
    ) {
        HeadStaff headStaff = headStaffService.getByUid(headStaffUid);
        if (null == headStaff) return R.fail("操作人信息获取失败");

        boolean b = appVersionService.haveVersionCodeOrName(id, os, versionCode, versionName);
        if (b) return R.fail("版本号或版本名重复，不能操作");

        AppVersion data = null;
        if (null == id || id <= 0) {
            data = new AppVersion();
            data.setEnabled(1);
            data.setAddStaffUid(headStaffUid);
            data.setAddStaffName(headStaff.getUserName());
        } else {
            data = appVersionService.getById(id);
            if (null == data) return R.fail("获取失败");
        }
        data.setOs(os);
        data.setVersionCode(versionCode);
        data.setVersionName(versionName);
        data.setDownloadUrl(downloadUrl);
        data.setContent(content);
        data.setForceStatus(forceStatus);
        data.setSelfStatus(selfStatus);
        data.setLastStaffUid(headStaffUid);
        data.setLastStaffName(headStaff.getUserName());
        appVersionService.saveOrUpdate(data);
        return R.ok();
    }

    @ApiOperation(value = "获取信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        AppVersion data = appVersionService.getById(id);
        if (null == data) return R.fail("获取失败");

        return R.ok(data);
    }

    @ApiOperation(value = "更新启用状态")
    @PostMapping(value = "/updateEnable", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnable(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "启用状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        HeadStaff headStaff = headStaffService.getByUid(headStaffUid);
        if (null == headStaff) return R.fail("操作人信息获取失败");

        Date time = DateUtil.getNowDatetime();

        long l = appVersionService.updateEnable(id, enabled, time, headStaff.getUid(), headStaff.getUserName());
        if (l <= 0) return R.fail("更新失败");
        return R.ok();
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id
    ) {
        long l = appVersionService.dropBean(id);
        if (l <= 0) return R.failDel();
        return R.ok();
    }

    @ApiOperation(value = "获取app最新版本")
    @PostMapping(value = "/getLatestApp", produces = "application/json; charset=UTF-8")
    public R<Object> getLatestApp(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "手机系统 1.安卓 2.ios") @RequestParam(value = "os") Integer os
    ) {
        List<AppVersion> list = appVersionService.getMaxCodeByOsAndVersionCode(os, null, true);
        if (CollectionUtils.isNotEmpty(list))
            return R.ok(list.get(0));

        return R.ok();
    }

}
